<?php

class SendNewFeedbackEmailTask extends sfBaseTask
{
    protected function configure()
    {
        $this->addOptions(array(
            new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environement', 'prod'),
        ));
        
        $this->namespace = 'updates';
        $this->name = 'user-update-email';
        $this->briefDescription = 'Send new feedback email to users who have requested it';
        $this->detailedDescription = '';
    }
    
    protected function execute($arguments = array(), $options = array())
    {
        LogHelper::log("Checking for new feedback emails to send.");

        // Required to allow database queries
        $databaseManager = new sfDatabaseManager($this->configuration);
        
        // Look for all users who request emails
        $usersWithEmailsConfigured = Doctrine_Core::getTable('User')
                ->createQuery('u')
                ->where('u.active = 1')
                ->andWhere('u.new_feedback_email_type <> ?', 'none')
                ->andWhere('u.last_new_feedback_email_date IS NOT NULL')
                ->execute();
        
        foreach ($usersWithEmailsConfigured as $userWithEmailsConfigured) {
            if ($userWithEmailsConfigured->getNewFeedbackEmailType() == 'daily'
                || ($userWithEmailsConfigured->getNewFeedbackEmailType() == 'weekly' && date('N') == 1))
            {
                LogHelper::log("Checking for new feedback items for " . $userWithEmailsConfigured->getEmail());
                
                $feedbackItems = Doctrine_Core::getTable('Feedback')
                        ->createQuery('f')
                        ->innerJoin('f.Source s')
                        ->innerJoin('s.UserSource us')
                        ->where('s.disabled = 0')
                        ->andWhere('us.disabled = 0')
                        ->andWhere('us.user_id = ?', $userWithEmailsConfigured->getId())
                        ->andWhere('f.retrieved_date > ?', $userWithEmailsConfigured->getLastNewFeedbackEmailDate())
                        ->orderBy('s.id, f.retrieved_date DESC, f.id DESC')
                        ->execute();
                
                // Only bother going on & setting up the email if there were some feedback items to show
                if ($feedbackItems->count() > 0) {
                    LogHelper::log($userWithEmailsConfigured->getEmail() . " has new feedback items to send.");
                    
                    $emailHtml = "<p>Here are your " . $userWithEmailsConfigured->getNewFeedbackEmailType() . " <a href='http://fydal.com'><strong>Fydal</strong></a> updates.</p>";
                    
                    // We'll need the sources list for the filter options, as a lookup
                    $userSources = Doctrine_Core::getTable('UserSource')
                        ->createQuery('us')
                        ->innerJoin('us.Source s')
                        ->where('s.disabled = 0')
                        ->andWhere('us.disabled = 0')
                        ->andWhere('us.user_id = ?', $userWithEmailsConfigured->getId())
                        ->execute();
                    $userSourceBySourceId = array();
                    foreach ($userSources as $userSource) {
                        $userSourceBySourceId[$userSource->getSourceId()] = $userSource;
                    }
                    
                    $lastSource = null;
                    
                    foreach ($feedbackItems as $feedbackItem) {
                        
                        // Provide a new header if we're now going through a new source's feedback
                        if ($lastSource === NULL || $lastSource->getId() != $feedbackItem->getSourceId()) {
                            
                            if ($lastSource !== NULL) {
                                $emailHtml .= "</ul>";
                                $emailHtml .= "</div>";
                            }
                            
                            $lastSource = $feedbackItem->Source;
                            
                            $userSource = $userSourceBySourceId[$lastSource->getId()];
                            
                            // Construct the source's header
                            $emailHtml .= "<div style='margin-top: 19px; padding: 19px; background-color: whiteSmoke; border: 1px solid #EEE; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);'>";
                            $emailHtml .= "<div>";
                            $emailHtml .= "<div style='float: left; padding-left: 10px;'>" . DisplayHelper::printIconWithRoundedCorners($userSource->Source->getFinalIconUrl(), false, true) . "</div>";
                            $emailHtml .= "<a style='float: right; display: block;' href='http://fydal.com/feedback/list?sourceId=" . $lastSource->getId() . "'><button style=\"display: inline-block; padding: 4px 10px 4px; margin-bottom: 0; font-size: 13px; line-height: 18px; color: #333; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75); vertical-align: middle; cursor: pointer; background-color: whiteSmoke; background-image: -ms-linear-gradient(top, white, #E6E6E6); background-image: -webkit-gradient(linear, 0 0, 0 100%, from(white), to(#E6E6E6)); background-image: -webkit-linear-gradient(top, white, #E6E6E6); background-image: -o-linear-gradient(top, white, #E6E6E6); background-image: linear-gradient(top, white, #E6E6E6); background-image: -moz-linear-gradient(top, white, #E6E6E6); background-repeat: repeat-x; border: 1px solid #CCC; border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25); border-color: #E6E6E6 #E6E6E6 #BFBFBF; border-bottom-color: #B3B3B3; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; filter: progid:dximagetransform.microsoft.gradient(startColorstr='#ffffff', endColorstr='#e6e6e6', GradientType=0); filter: progid:dximagetransform.microsoft.gradient(enabled=false); -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05); -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05); padding: 9px 14px; font-size: 15px; line-height: normal; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; background-color: #0074CC; background-image: -ms-linear-gradient(top, #08C, #05C); background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#08C), to(#05C)); background-image: -webkit-linear-gradient(top, #08C, #05C); background-image: -o-linear-gradient(top, #08C, #05C); background-image: -moz-linear-gradient(top, #08C, #05C); background-image: linear-gradient(top, #08C, #05C); background-repeat: repeat-x; border-color: #05C #05C #003580; border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25); filter: progid:dximagetransform.microsoft.gradient(startColorstr='#0088cc', endColorstr='#0055cc', GradientType=0); filter: progid:dximagetransform.microsoft.gradient(enabled=false); color: white; text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);\">View all feedback</button></a>";
                            $emailHtml .= "<h3 style='float: left; margin-top: 15px; margin-left: 15px; font-size: 14px;'>" . $userSource->getDisplayName() . " " . DisplayHelper::createProviderIconHtml($userSource->Source->getProvider(), "height: 15px; width: 15px;", true) . "</h3>";
                            $emailHtml .= "</div>";
                            $emailHtml .= "<ul style='clear: both; padding-top: 10px; margin-bottom: 0px;'>";
                        }
                        
                        // Construct the feedback item
                        $emailHtml .= "<li style='margin-top: 5px; margin-bottom: 5px;'>";
                        $emailHtml .= ($feedbackItem->getTitle() ? "<strong>" . $feedbackItem->getTitle() . "</strong>" : "");
                        $emailHtml .= ($feedbackItem->getTitle() && $feedbackItem->getContent() ? " - " : "") . ($feedbackItem->getContent() ? $feedbackItem->getContent() : "");
                        $emailHtml .= "<br />";
                        $emailHtml .= ($feedbackItem->getRating() > 0 ? "<em>" . $feedbackItem->getRating() . " star" . ($feedbackItem->getRating() > 1 ? "s" : "") . "</em> | " : "");
                        $emailHtml .= "<em>" . DateHelper::fuzzyDateDayOnly($feedbackItem->getSubmittedDate()) . "</em>";
                        $emailHtml .= "</li>";
                    }
                    
                    $emailHtml .= "</ul>";
                    $emailHtml .= "</div>";
                    
                    $emailHtml .= "<p>To unsubscribe from these email updates or to change your sources, visit your <a href='http://fydal.com/feedback/setup'><strong>Fydal</strong> setup</a> page.</p>";
                    
                    //echo $emailHtml;exit;
                    
                    EmailHelper::send($userWithEmailsConfigured->getEmail(), "Your " . $userWithEmailsConfigured->getNewFeedbackEmailType() . " Fydal updates", $emailHtml);
                    
                    echo "Email sent to " . $userWithEmailsConfigured->getEmail() . "." . PHP_EOL;
                    LogHelper::log("Email successfully sent to " . $userWithEmailsConfigured->getEmail());
                }
                
                $userWithEmailsConfigured->setLastNewFeedbackEmailDate(date("Y-m-d H:i:s"));
                $userWithEmailsConfigured->save();
            }
        }
        
        echo "Done." . PHP_EOL;
    }
}